# coding: utf-8
# author: t_wolf


from core import *


class Base(object):
    # 动态获取,每次请求更新Thread_dict
    header = None            # 请求头
    cookies = None           # 用户Cookie
    session = None           # 用户会话
    request = None           # 请求参数
    pattern = None           # 请求url中的正则匹配项

    # 注册为app_adapter对象时注入
    url_origin = None        # 源监听Url
    pages = None             # 页面渲染对象
    redis = None             # 注入redis对象,使用和session同样的库
    path = None              # 当前应用绝对路径
    vars = None              # 个人自定义变量
    log = LOGGER             # 日志对象
    db = DbUtil              # 数据库连接对象

    cookie_expired = 0       # cookie过期时间
    frame_worker = ""        # 框架对象类型

    # 运行时变量
    belong_app = ""

    # 定时任务相关
    s = 1                    # 秒
    ms = 0                   # 毫秒
    cron = "*/60 * * * *"    # cron
    counts = 0               # 执行次数

    # 预定义的操作
    def post(self):
        logger.error("[%s]: no implement method request <post>." % self.__class__.__name__)
        raise web.internalerror(CONFIGS.MESSAGE.no_implement_method)

    def get(self):
        logger.error("[%s]: no implement method request <get>." % self.__class__.__name__)
        raise web.internalerror(CONFIGS.MESSAGE.no_implement_method)

    def put(self):
        logger.error("[%s]: no implement method request <put>." % self.__class__.__name__)
        raise web.internalerror(CONFIGS.MESSAGE.no_implement_method)

    def delete(self):
        logger.error("[%s]: no implement method request <delete>." % self.__class__.__name__)
        raise web.internalerror(CONFIGS.MESSAGE.no_implement_method)

    def session_get(self, key):
        return self.session[key] if self.session_has(key) else None

    def session_has(self, key):
        return True if key in self.session else False

    def session_put(self, key, value):
        if self.session_has(key):
            self.session_update({key: value})
        else:
            self.session.setdefault(key, value)

    def session_update(self, pair):
        self.session.update(pair)

    def session_dump(self):
        return self.session.items()

    def cookies_get(self, key):
        return self.cookies[key] if self.cookies_has(key) else None

    def cookies_has(self, key):
        return True if key in self.cookies else False

    def cookies_put(self, key, value, expired=None):
        expired = self.cookie_expired if not expired else expired
        return web.setcookie(key, value, expired)

    def cookies_dump(self):
        return self.cookies.items()

    def header_get(self, key):
        return self.header[key] if self.header_has(key) else None

    def header_has(self, key):
        return True if key in self.header else False

    def header_put(self, key, value):
        return web.header(key, value)

    def header_dump(self):
        return self.header.items()

    def request_get(self, key):
        return self.request[key] if self.request_has(key) else None

    def request_dump(self):
        return self.request.items()

    def request_has(self, key):
        return True if key in self.request else False

    def redirect(self, _path, absolute=False):
        web.seeother(_path, absolute)

    def set_obj_to_redis(self, key, item):
        return self.redis.set(key, data_encode(item))

    def get_obj_from_redis(self, key):
        return data_decode(self.redis.get(key))

    # through threads
    def push_to_thread_share(self, **kwargs):
        [setattr(SHARE_SPACE.thread, _key, _value) for _key, _value in kwargs.items()]

    # through threads
    def pull_from_thread_share(self, _key):
        return SHARE_SPACE.thread.get(_key, None)

    # through processes
    def push_to_process_share(self, _key, _value, _expired=None):
        return SHARE_SPACE.process.set(_key, _value, _expired)

    # through processes
    def pull_from_process_share(self, _key):
        return SHARE_SPACE.process.get(_key, None)
